package com.controller.admin;

import com.controller.BaseController;
import com.entity.Goods;
import com.github.pagehelper.PageInfo;
import com.service.imple.home.GoodsImple;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品管理
 */

@Controller
@RequestMapping("admin/work")
public class GoodsManageController extends BaseController<GoodsImple> {


    /**
     * 查询
     * @return
     */
    @RequestMapping("toGoodsManage")
    public String toGoodsManage(){
        return "center/admin/goods/goodsManage";
    }

    @RequestMapping("goodsManage")
    public String goodsManage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum, ModelMap modelMap,String name){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("goodsName",name);
        PageInfo<Goods> pageInfo = BaseService.selectByPage(map);
        modelMap.put("pageInfo",pageInfo);
        return "center/admin/goods/goodsPage";
    }

    /**
     * 删除
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("delete")
    public String deleteGoods(Integer id, ModelMap modelMap){
        BaseService.delete(id);
        return "center/admin/goods/goodsManage";
    }

    /**
     * 修改
     * @param
     * @return
     */
    @RequestMapping("ToAlter")
    public String ToAlter(Integer id,ModelMap modelMap){
        Goods goods = new Goods();
        goods.setId(id);
        List<Goods> list= BaseService.selectAll(goods);

        modelMap.put("list",list);
        return "center/admin/goods/index";
    }

    @RequestMapping("update")
    @ResponseBody
    public Map<String,Object> alter(Goods goods){
        Map<String,Object> map = new HashMap<>();
        Goods goods1 = new Goods();
        goods1.setAddTime(new Date());
        goods1.setId(goods.getId());
        List<Goods> list = BaseService.selectAll(goods1);
        if (goods.getGoodsImg()==null){
            list.get(0).setGoodsImg(list.get(0).getGoodsImg());
        }else {
            list.get(0).setGoodsImg(goods.getGoodsImg());
        }

        int count = BaseService.update(list.get(0));
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    /**
     * 修改图片
     * @param file
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping("toFile")
    @ResponseBody
    public Map<String, Object> toFile( @RequestParam("fileImg")MultipartFile file, HttpSession session) throws IOException {

        //获取上传文件的名字
        String filename = file.getOriginalFilename();
        String newFileName = new Date().getTime()+filename.substring(filename.lastIndexOf("."));
        //上传部署后的目录中getRealPath
        String basePath = session.getServletContext().getRealPath("/public/images/");
        File baseFile = new File(basePath);
        if (!baseFile.exists()){//判断文件夹是否存在
            baseFile.mkdir();//创建文件夹
        }
        String savePath = basePath+newFileName;
        System.out.println(savePath);
        file.transferTo(new File(basePath+newFileName));

        Map<String,Object> map = new HashMap<>();
        map.put("url","http://localhost:8080/public/images/"+newFileName);
        map.put("url1","/"+newFileName);
        return map;
    }

    /**
     * 添加商品
     * @param
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(){
        return "center/admin/goods/addGoods";
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> add( Goods goods ) {
        goods.setAddTime(new Date());
        int  count = BaseService.insert(goods);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    /**
     * 修改商品详情信息
     * @return
     */
    @RequestMapping("toDescription")
    public String toDescription(Integer id,ModelMap modelMap){
        Goods goods = new Goods();
        goods.setId(id);
        List<Goods> list= BaseService.selectAll(goods);
        modelMap.put("list",list);
        return "center/admin/goods/dome";
    }

    @RequestMapping("description")
    @ResponseBody
    public Map<String,Object> description(Integer id,String description){
        Map<String,Object> map = new HashMap<>();
        Goods goods = new Goods();
        goods.setId(id);
        List<Goods> list = BaseService.selectAll(goods);
        list.get(0).setDescription(description);
        int count = BaseService.update(list.get(0));
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

}
